package edu.fiu.cis.personalize.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.fiu.cis.personalize.shared.FieldVerifier;
import edu.fiu.cis.personalize.shared.GoogleDocument;
import edu.fiu.cis.personalize.shared.User;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class PersonalizedSearch implements EntryPoint {

	private Button sendButton;

	private TextBox nameField;

	private Label errorLabel;
	
	private User user = new User(1,"abhijith");

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final PersonalizedSearchServiceAsync searchService = GWT
			.create(PersonalizedSearchService.class);

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		sendButton = new Button("Search");
		nameField = new TextBox();
		errorLabel = new Label();

		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("nameFieldContainer").add(nameField);
		RootPanel.get("sendButtonContainer").add(sendButton);
		RootPanel.get("errorLabelContainer").add(errorLabel);

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);

		// Add a handler to send the name to the server
		SearchHandler handler = new SearchHandler();
		sendButton.addClickHandler(handler);
		nameField.addKeyUpHandler(handler);
	}

	private void submitQuery() {
		// First, we validate the input.
		errorLabel.setText("");
		String textToServer = nameField.getText();
		if (!FieldVerifier.isValidName(textToServer)) {
			errorLabel.setText(FieldVerifier.getErrorMessage(textToServer));
			return;
		}
		// Then, we send the input to the server.
		sendButton.setEnabled(false);
		searchService.search(textToServer,
				new DefaultAsyncCallback<ArrayList<GoogleDocument>>() {
					public void onSuccess(ArrayList<GoogleDocument> result) {
						VerticalPanel panel = new VerticalPanel();
						RootPanel.get("container").clear();
						RootPanel.get("container").add(panel);
						for (GoogleDocument document : result) {
							Anchor anchor = new Anchor(document.getTitle(), document.getURL());
							anchor.addClickHandler(new AnchorClickHandler(user,document.getURL()));
							panel.add(anchor);
						}
						sendButton.setEnabled(true);
					}
				});

	}

	class AnchorClickHandler implements ClickHandler{
		private User user;
		private String url;
		
		public AnchorClickHandler(User user, String url) {
			this.url = url;
			this.user = user;
		}

		@Override
		public void onClick(ClickEvent event) {
			searchService.clickGoogleDocument(url, user, new VoidCallback());
		}
		
	}
	// Create a handler for the sendButton and nameField
	class SearchHandler implements ClickHandler, KeyUpHandler {
		/**
		 * Fired when the user clicks on the sendButton.
		 */
		public void onClick(ClickEvent event) {
			submitQuery();
		}

		/**
		 * Fired when the user types in the nameField.
		 */
		public void onKeyUp(KeyUpEvent event) {
			if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
				submitQuery();
			}
		}
	}
}
